﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using Ooganizer.Server;

namespace Ooganizer.Platform
{
    /// <summary>
    /// Installation Specific Settings. "HKLM\Software\Ooganizer",
    /// The Installer must put the correct values in there so that we know where our files
    /// are. ~Since now some of our assemblies are in the GAC, we require this.
    /// </summary>
    public class InstallationSpec
    {
        /// <summary>
        /// private statics
        /// </summary>
        private static string m_strInstallPath = "";
        private static string m_strPlatformPath = "";
        private static string m_strServerPath = "";
        private static string m_strLogPath = "";
        private static RegistryKey s_OogyRootKey;

        /// <summary>
        /// Public Properties
        /// </summary>
        public static string InstallPath { get { return m_strInstallPath; } }
        public static string PlatformPath { get { return m_strPlatformPath; } }
        public static string ServerPath { get { return m_strServerPath; } }
        public static string LogPath { get { return m_strLogPath; } }
 
        /// <summary>
        /// Static Constructor, immediatly gets our installation specific variables
        /// </summary>
        static InstallationSpec()
        {
            s_OogyRootKey = Registry.LocalMachine.OpenSubKey("Software\\Ooganizer",false);
            if (s_OogyRootKey != null)
            {
                object keyvalue = null;

                keyvalue = s_OogyRootKey.GetValue("InstallPath");
                if (isValidStr(keyvalue))
                    m_strInstallPath = keyvalue.ToString();

                keyvalue = s_OogyRootKey.GetValue("PlatformPath");
                if (isValidStr(keyvalue))
                    m_strPlatformPath = keyvalue.ToString();

                keyvalue = s_OogyRootKey.GetValue("ServerPath");
                if (isValidStr(keyvalue))
                    m_strServerPath = keyvalue.ToString();

                keyvalue = s_OogyRootKey.GetValue("LogPath");
                if (isValidStr(keyvalue))
                    m_strLogPath = keyvalue.ToString();
            }
            else
            {
                LogServerClientWrapper logWrapper = new LogServerClientWrapper(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

                logWrapper.Log.Error(string.Format("{0}() - Couldn't Open Software\\Ooganizer Registry Key for Reading",
                          System.Reflection.MethodBase.GetCurrentMethod().Name));
            }
        }

        /// <returns>Returns true if passed in object is valid and not ""</returns>
        private static bool isValidStr(object oToValidate)
        {
            if ((oToValidate != null) && (oToValidate.ToString() != ""))
                return true;
            else
                return false;
        }
    }
}
